﻿//JumbotCms_4.1 Final
using System;
using System.Web;
using System.Data;
using System.Collections;
namespace JumbotCms.Common
{
    public class NewsCollFilter
    {
        public NewsCollFilter()
        {
        }
        /// <summary>
        /// 过滤内容
        /// </summary>
        /// <param name="doh"></param>
        /// <param name="PageBody">原文内容</param>
        /// <param name="ItemId">项目ID</param>
        /// <param name="FilterObject">Title或Body</param>
        /// <returns></returns>
        public string FilterBody(Jumbot.DataOper.DbOperHandler doh, string PageBody, string ItemId, string FilterObject)
        {
            doh.Reset();
            string sqlStr = "Select * FROM [jcms_module_article_collfilters] WHERE ([ItemId]=" + ItemId + " OR [PublicTf]=1)";
            if (FilterObject == "Title")
                sqlStr += " AND [Filter_Object]=0";
            else
                sqlStr += " AND [Filter_Object]=1";
            sqlStr += " AND [Flag]=1 ORDER BY Id";
            doh.SqlCmd = sqlStr;
            DataTable dtCollItemFilters = doh.GetDataTable();
            if (dtCollItemFilters.Rows.Count == 0)
                return PageBody;
            for (int i = 0; i < dtCollItemFilters.Rows.Count; i++)
            {
                string fId = dtCollItemFilters.Rows[i]["Id"].ToString();
                string fType = dtCollItemFilters.Rows[i]["Filter_Type"].ToString();
                string fContent = dtCollItemFilters.Rows[i]["Filter_Content"].ToString();
                string fsString = dtCollItemFilters.Rows[i]["FisString"].ToString();
                string foString = dtCollItemFilters.Rows[i]["FioString"].ToString();
                string fReplace = dtCollItemFilters.Rows[i]["Filter_Rep"].ToString();
                if (fType == "0")//简单替换
                    PageBody = PageBody.Replace(fContent, fReplace);
                else
                {
                    NewsCollection nc = new NewsCollection();
                    System.Collections.ArrayList replaceArray = nc.GetArray(PageBody, fsString, foString);
                    if (replaceArray.Count == 0)
                        return PageBody;
                    //if (replaceArray[0].ToString() == "$StartFalse$" || replaceArray[0].ToString() == "$EndFalse$")
                        //return PageBody;
                    for (int j = 0; j < replaceArray.Count; j++)
                    {
                        PageBody = PageBody.Replace(replaceArray[j].ToString(), fReplace);
                    }
                }
            }
            dtCollItemFilters.Clear();
            dtCollItemFilters.Dispose();
            return PageBody;

        }
    }
}
